//
//  AppHtmlViewController.m
//  减肥,美容老中医偏方
//
//  Created by Appying on 14-9-17.
//  Copyright (c) 2014年 陈朕. All rights reserved.
//

#import "AppHtmlViewController.h"

@interface AppHtmlViewController ()

@end

@implementation AppHtmlViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        appBean = [AppBean shareInstence];
//        self.title = appBean.title;
        
        UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, boundswidth-120, 44)];
        navLabel.text = appBean.title;
        navLabel.textColor = [UIColor colorWithRed:1 green:0.94 blue:0.82 alpha:1];
        navLabel.font = [UIFont systemFontOfSize:18];
        navLabel.textAlignment = NSTextAlignmentCenter;
        self.navigationItem.titleView = navLabel;
        
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bgIMG"]];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // 创建表
    [BaseAllCase createTable];
    self.array = [BaseAllCase getAllCaseTitle];
    [self initUI];
    [self getCollectDate];
    
    
   
}
-(void)initUI{
    //导航条左边返回按钮
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0, 50, 44);
    backButton.showsTouchWhenHighlighted = YES;// 按钮按下时会发光
    [backButton setBackgroundImage:[UIImage imageNamed:@"image_back"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    // 导航条右边的收藏按钮
    _collectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _collectButton.frame = CGRectMake(0, 0, 50, 44);
//    _collectButton.backgroundColor = [UIColor redColor];
    _collectButton.showsTouchWhenHighlighted = YES;// 按钮按下时会发光
//    [_collectButton setTitle:@"收藏" forState:UIControlStateNormal];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:_collectButton];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    appBean = [AppBean shareInstence];
    
    UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, boundswidth, boundsheight)];
    webView.backgroundColor = [UIColor clearColor];
    webView.opaque = NO;
    
    NSURL *url = [NSURL URLWithString:appBean.url];
    [webView loadRequest:[NSURLRequest requestWithURL:url]];
    [self.view addSubview:webView];
}
// 获取收藏的数据
-(void)getCollectDate{
    _nameArray = [[NSMutableArray alloc] init];
    _contentArray = [[NSMutableArray alloc] init];
    _array = [[NSMutableArray alloc] init];
    
    // 创建表 FMDB数据存储
    [BaseAllCase createTable];
    _array = [BaseAllCase getAllCaseTitle];
    if ([_array count] != 0) {// 如果数据库里面的数据不等于零
        
        for (int i = 0 ; i < self.array.count; i++) {// 遍历出具库中所有的数组
            [_nameArray addObject:[_array objectAtIndex:i]];
        }
        _nameArr= [[NSMutableArray alloc] initWithCapacity:0];
        for (int j = 0 ; j < _nameArray.count; j++) {
            _name = [_nameArray objectAtIndex:j];
            _nameStr= [NSString stringWithFormat:@"%@",_name.caseTitle];
            [_nameArr addObject:_nameStr];
        }
        for (int g = 0; g < _nameArr.count; g++) {
            // 右边按钮的点击事件
            if ([_nameArr containsObject:appBean.title]) {//已经收藏
                [_collectButton setBackgroundImage:[UIImage imageNamed:@"image_collectFinish"] forState:UIControlStateNormal];
                _collectButton.enabled = NO;
            }else{// 没有收藏
                [_collectButton setBackgroundImage:[UIImage imageNamed:@"image_collectNo"] forState:UIControlStateNormal];
                [_collectButton addTarget:self action:@selector(collectButtonClick:) forControlEvents:UIControlEventTouchUpInside];
                
                _collectButton.showsTouchWhenHighlighted = YES;// 按钮按下时会发光
            }
        }
    }else{
        [_collectButton setBackgroundImage:[UIImage imageNamed:@"image_collectNo"] forState:UIControlStateNormal];
        _collectButton.showsTouchWhenHighlighted = YES;// 按钮按下时会发光
        [_collectButton addTarget:self action:@selector(collectButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        _array = [BaseAllCase getAllCaseTitle];
    }
}
-(void)collectButtonClick:(UIButton *)sender{// 点击按钮收藏
    
    [_collectButton setBackgroundImage:[UIImage imageNamed:@"image_collectFinish"] forState:UIControlStateNormal];
    
    AllCase *title = [[AllCase alloc] init];
    title.caseTitle = appBean.title;
    title.caseContent = appBean.url;
    
    [BaseAllCase insertFruits:title];
    
    // 每次插入之后从数据库中获取所有数据
    self.array = [BaseAllCase getAllCaseTitle];

    _alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"您已经收藏成功，可以在“我的收藏”里面查看哦！^_^" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
    [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(performDismiss:) userInfo:nil repeats:NO];
    [_alertView show];
    
    _collectButton.enabled = NO;

}
-(void)performDismiss:(NSTimer *)timer{
    [_alertView dismissWithClickedButtonIndex:0 animated:NO];
}
-(void)backButtonClick:(UIButton *)sender{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
